#include <p24FJ32GA002.h>
#include <stdio.h>
#include <stdlib.h>

char *itoa(char *buffer,int i) {
	unsigned int n;
	unsigned int negate = 0;
	int c = 6;
	if (i < 0) {
		negate=1;
		n = -i;
		} else if (i == 0) {
		buffer[0] = '0';
		buffer[1] = 0;
		return buffer;
		} else {
		n = i;
		}
	buffer[c--] = 0;
		do {
		buffer[c--] = (n % 10) + '0';
		n = n / 10;
		} 
	while (n);
	if (negate) {
		buffer[c--] = '-';
		}
	return &buffer[c+1];
}

// itoa buffer
char buffer[10];

void putU1Dec(unsigned char dec) 
{
putsU1(itoa(buffer, dec));
}

void InitADC(void){
//AD1PCFGLbits.PCFG12	= 0;			//AN12 ADC pin
//AD1CON2bits.VCFG = 0;			//Voltage Reference bits
//AD1CON3bits.ADCS = 255;		//A/D Conversion Clock bits 256*Tcy
//AD1CON1bits.SSRC = 6;				// Sample Clock Source: Timer 3 starts conversion
//AD1CON3bits.SAMC = 31;
   AD1CON1 = 0x00E0;                                                // SSRC<3:0> = 111 implies internal
                                                                   // counter ends sampling and starts
                                                                   // converting.
   AD1CHS = 0x30C;                                                    // Connect AN0 as CH0 input.
   AD1CSSL = 0;
   AD1CON3 = 0x1F02;                                                // Sample time = 31Tad,
                                                                   // Tad = 2 Tcy
   AD1CON2 = 0;
   AD1CON1bits.ADON = 1;                                            // turn ADC ON

}

void ConvertADC(int adcno){
if (adcno==12)	{
				AD1CON1bits.SAMP = 1;                                        // start sampling then...
                                                                   // after 31Tad go to conversion
       			while (!AD1CON1bits.DONE);                                    // conversion done?
       			//putsLCD("adcno is 12");
				}

//buffi = ADC1BUF0;
//putU1Dec = ADC1BUF0;
//str = itoa(buffi, str, 10); // 10 - decimal; 
//return ADC1BUF0;                                // yes then get ADC value
}